﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Trade.DBUtility
{
    public class MailCommon
    {
        #region 得到Cid的ContentID
        /// <summary>
        /// 得到Cid的ContentID
        /// </summary>
        /// <param name="strBody"></param>
        /// <param name="?"></param>
        /// <returns></returns>
        public List<string> GetCidContentID(string strMailBody)
        {
            List<string> strCidList = new List<string>();
            if (strMailBody != null && strMailBody != string.Empty)
            {
                int iCidStartIndex = -1;
                int iCidEndIndex = -1;
                string strCid;
                while (true)
                {
                    //src=3D"cid:002b01c90cbe$e40e9410$8201a8c0@KFB011" IMG的SRC显示为这样的时候需要转换为本地
                    iCidStartIndex = strMailBody.ToLower().IndexOf("\"cid:", iCidStartIndex + 1);
                    if (iCidStartIndex < 0)
                        break;
                    iCidEndIndex = strMailBody.IndexOf("\"", iCidStartIndex + 4);
                    if (iCidEndIndex < 0)
                        break;
                    //取得的CID
                    strCid = strMailBody.Substring(iCidStartIndex + 5, iCidEndIndex - iCidStartIndex - 5);
                    //添加CID
                    strCidList.Add(strCid);
                    //
                    strMailBody = strMailBody.Replace(strMailBody.Substring(iCidStartIndex, iCidEndIndex - iCidStartIndex + 1), "\"" + " " + "\"");
                }
            }
            return strCidList;
        }
        #endregion

        public static string GetMD5HashFromFile(string fileName)
        {
            try
            {
                FileStream file = new FileStream(fileName, FileMode.Open);
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(file);
                file.Close();
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("处到文件的MD5信息出错:" + ex.Message);
            }
        }
    }
}
